package web.beans;

import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import java.io.Serializable;

/**
 * Created by vanches on 16.1.15.
 */
public class ConfigApplicationBean implements Serializable {
    private String localeApplication="ru";
    private UserBean userBean;


    public void checkUser(ComponentSystemEvent event){
        if(userBean.getAccessLevel()==0){
            FacesContext context=FacesContext.getCurrentInstance();
            ConfigurableNavigationHandler handler= (ConfigurableNavigationHandler) context.getApplication().getNavigationHandler();
            handler.performNavigation("quest");
        }

    }
    public void checkAdmin(ComponentSystemEvent event){
        if(userBean.getAccessLevel()==0 || userBean.getAccessLevel()==1){
            FacesContext context=FacesContext.getCurrentInstance();
            ConfigurableNavigationHandler handler= (ConfigurableNavigationHandler) context.getApplication().getNavigationHandler();
            handler.performNavigation("index");
        }
    }

    public void localeSetRu(){
       userBean.setLocale("ru");
    }
    public void localeSetEn(){
        userBean.setLocale("en");
    }

    //region GET and SET

    public String getLocaleApplication() {

        if(!userBean.getLocale().equals(localeApplication) && userBean.getLocale()!=null){
            localeApplication=userBean.getLocale();
        }
            return localeApplication;
    }

    public void setLocaleApplication(String localeApplication) {
        this.localeApplication = localeApplication;
    }

    public UserBean getUserBean() {
        return userBean;
    }

    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    //endregion
}
